package com.ares.basic.http;

import lombok.Getter;
import org.apache.http.Header;
import org.apache.http.HttpStatus;

import java.util.Arrays;

@Getter
public class HttpResult {

    public static final int HTTP_EXECUTE_SUCCESS = HttpStatus.SC_OK;

    public static final int HTTP_THROW_EXCEPTION = -1;

    /**
     * http 状态码
     */
    private final int httpStatus;

    /**
     * 错误信息
     */
    private final String errorMsg;

    /**
     * response
     */
    private final String responseBody;

    /**
     * 处理消耗的毫秒数
     */
    private final double consumeMillis;

    /**
     * http状态是否异常
     *
     * @return
     */
    public boolean isHttpThrowException() {
        return this.httpStatus == HTTP_THROW_EXCEPTION;
    }

    /**
     * response headers
     */
    private final Header[] headers;

    public HttpResult(String responseBody, double consumeMillis, Header[] headers) {
        this(HTTP_EXECUTE_SUCCESS, null, responseBody, consumeMillis, headers);
    }

    public HttpResult(int httpStatus, String errorMsg, double consumeMillis, Header[] headers) {
        this(httpStatus, errorMsg, null, consumeMillis, headers);
    }

    public HttpResult(int httpStatus, String errorMsg, String responseBody, double consumeMillis, Header[] headers) {
        this.httpStatus = httpStatus;
        this.errorMsg = errorMsg;
        this.responseBody = responseBody;
        this.consumeMillis = consumeMillis;
        this.headers = headers;
    }

    /**
     * http状态是否成功
     *
     * @return
     */
    public boolean isHttpSuccess() {
        return this.httpStatus == HTTP_EXECUTE_SUCCESS;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("HttpResult{");
        sb.append("httpStatus=").append(httpStatus);
        sb.append(", errorMsg='").append(errorMsg).append('\'');
        sb.append(", responseBody='").append(responseBody).append('\'');
        sb.append(", consumeMillis=").append(consumeMillis);
        sb.append(", headers=").append(Arrays.toString(headers));
        sb.append('}');
        return sb.toString();
    }

}
